package com.nepu.springboot.springbootrealtime.service.imp;


import com.nepu.springboot.springbootrealtime.bean.NameValue;
import com.nepu.springboot.springbootrealtime.mapper.PublisherMapper;
import com.nepu.springboot.springbootrealtime.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author chenshuaijun
 * @create 2022-11-06 10:43
 */
@Service
public class PublisherServiceImp implements PublisherService {
    @Autowired
    private PublisherMapper publisherMapper;

    @Override
    public Map<String, Object> dauRealTime(String td) {
        Map<String, Object> result = publisherMapper.dauRealTime(td);
        return result;
    }

    @Override
    public List<NameValue> statsByItem(String itemName, String date, String t) {
        List<NameValue> result = publisherMapper.statsByItem(itemName, date, processT(t));
        // 对结果进行转化
        List<NameValue> endResult = normativeData(result, t);
        return endResult;
    }

    @Override
    public Map<String, Object> detailByItem(String itemName, String date, String pageNo, String pageSize) {
        // 对pageNo进行处理
        int newPageNo = (Integer.parseInt(pageNo) - 1) * Integer.parseInt(pageSize);
        Map<String, Object> result = publisherMapper.detailByItem(itemName,date,newPageNo+"",pageSize);
        return result;
    }

    /**
     * 处理我们传入的字段，使其符合es的标准
     *
     * @param t 传入字段
     * @return 返回es中对应的字段
     */
    private String processT(String t) {
        if ("age".equals(t)) {
            return "user_age";
        } else if ("gender".equals(t)) {
            return "user_gender";
        } else {
            return null;
        }
    }

    private List<NameValue> normativeData(List<NameValue> result, String t) {
        if ("age".equals(t)) {
            double total20down = 0;
            double total20to29 = 0;
            double total30up = 0;
            for (NameValue nameValue : result) {
                int age = Integer.parseInt(nameValue.getName());
                if (age < 20) {
                    total20down += Double.parseDouble(nameValue.getValue().toString());
                } else if (age <= 29) {
                    total20to29 += Double.parseDouble(nameValue.getValue().toString());
                } else {
                    total30up += Double.parseDouble(nameValue.getValue().toString());
                }
            }
            result.clear();
            result.add(new NameValue("20岁以下", total20down));
            result.add(new NameValue("20岁至29岁", total20to29));
            result.add(new NameValue("30岁以上", total30up));
        } else if ("gender".equals(t)) {
            for (NameValue nameValue : result) {
                String name = nameValue.getName();
                if ("F".equals(name)) {
                    nameValue.setName("女");
                } else {
                    nameValue.setName("男");
                }
            }
        }
        return result;
    }
}
